<?php

require_once('config/maptrick.conf.php');
require_once(SITE_ROOT . 'lib/SmartyMaptrick.class.php');
require_once(SITE_ROOT . 'class/logic/Registration.class.php');
require_once(SITE_ROOT . 'class/bean/Team.class.php');
require_once(SITE_ROOT . 'class/bean/League.class.php');
require_once(SITE_ROOT . 'config/session.php');

$smarty = new SmartyMaptrick();

if (count($_POST) > 0 && ($_POST["rnd"] != $_SESSION["rnd_maptrick"] || $_POST["rnd"] == "" || !isset($_POST["rnd"]))) {
	$_SESSION = array();
	$_POST = array();
}

if (preg_match("/^[A-Za-z_-]{1,32}$/", $_POST['hidden'])) $hidden = $_POST['hidden'];

if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier']) && count($_POST) == 0) {
	try {
		$register = new Registration();
		$register->connect($_GET['oauth_token'], $_GET['oauth_verifier']);
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}

if ($hidden == 'login') {
	try {
		$register = new Registration();
		$callback = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
		$url = $register->login($callback);
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}
else if ($hidden == 'logout') {
	try {
		$register = new Registration();
		$register->logout();
	}
	catch (Exception $e) {
		$smarty->assign('message', $e->getMessage());
	}
}

if ($hidden == 'login') {
	header('Location: '. $url);
}
else {
	$smarty->assign('loggedIn', $_SESSION['login']);

	// assign the important attributes for the header
	$smarty->assign('title', 'Maptrick');
	$smarty->assign('home_active', 'active');

	$_SESSION["rnd_maptrick"] = hash_hmac("sha256", rand(1000000000, 9999999999), SCRIPT_PWD);
	$smarty->assign("rnd_maptrick", $_SESSION["rnd_maptrick"]);
	
	if ($_SESSION['login'] && isset($_SESSION['login_team'])) {
		$login_team = $_SESSION['login_team'];
		$smarty->assign('login_user', $login_team->getUserName());
		$smarty->assign('login_team', $login_team->getName());
		$smarty->assign('login_teamID', $login_team->getTeamID());
		if ($login_team->getLeague() != null) {
			$smarty->assign('login_leagueID', $login_team->getLeague()->getLeagueID());
			$smarty->assign('login_league', $login_team->getLeague()->getName());
		}
	}

	$smarty->display('header.tpl');
	
	$smarty->assign('news_title09', '09.04.2011, Training');
	$smarty->assign('news_text09', "<p>A table displaying the distribution of the available training types is now available. I hope to bring you a chart about the distribution soon.</p>");
	
	$smarty->assign('news_title08', '15.01.2011, oAuth authentification');
	$smarty->assign('news_text08', "<p>Since Hattrick reopened the CHPP program they also changed the login method. Maptrick is one of the first sites adapting to that oAuth authentification.</p><p>What does this mean for you?<br />You won't need your security code anymore. You just sign in with Hattrick on their site by clicking on \"Sign in with Hattrick\" above.<br />If you have any questions please just contact me.</p>");
	
	$smarty->assign('news_title07', '22.08.2009, Federation import');
	$smarty->assign('news_text07', "<p>I released an updated federation import site today. When you import a federation to see it in the Hall of Cool a summary will be shown. You can see the number of the managers at the last import, the number of members at the moment, which manager left and which manager joined the fed since the last import. Kudos to mk0111 for proposing this. Enjoy!</p>");
	
	$smarty->assign('news_title06', '05.06.2009, Federation & conference code');
	$smarty->assign('news_text06', "<p>Paul-Kringe founded a new Hattrick federation which concentrates on maptrick. The name is <a href=\"http://www.hattrick.org/Community/Federations/Federation.aspx?AllianceID=84183\">\"Coolness and Flag Updater Federation of Maptrick\" (Alliance ID: 84183)</a>. Everybody is welcome to join and help out in the federation.</p><p>In other news: I added the conference code for your team's rank in the Hall of Cool on the coolness page.</p>");
	
	$smarty->assign('news_title05', '27.02.2009, Conference code');
	$smarty->assign('news_text05', "<p>Now conference code is available on the coolness page. You can copy the code either for countries you are still missing or countries you've played already. Enjoy!</p>");
	
	$smarty->assign('news_title04', '17.02.2009, First graph available');
	$smarty->assign('news_text04', "<p>Today I can finally release the first graph. It shows you the active and waiting users in a country. I hope you like it.</p>");
	
	$smarty->assign('news_title03', '03.12.2008, Botmap available');
	$smarty->assign('news_text03', "<p><a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=404453\" target=\"_blank\">eBusiness</a> implemented the botmap for you again. He did a superb job in recreating an old, long missed feature of Maptrick.</p><p>You can see the distribution of botteams in your country, division and even in your league now. You can also take a look at the distributions of supporters in the Hattrick world.</p><p>A big round of applause to him!</p>");

	$smarty->assign('news_title02', '27.11.2008, Hall of Cool opens its doors');
	$smarty->assign('news_text02', "<p>It is finally here: The Hall of Cool!<br />Now you can compare your coolness rating with other teams from your country, division, league or federation. You can even compare with teams which started with you in the same month or year. The  possibilities seem endless since you can combine all of this options with each other!</p><p>This new version introduces a small nice feature as well: You can directly link to your coolness rating (if your team is in the database) or to the Hall of Cool view of your choice! Get more information how to do it on this page under \"How to\".</p><p>Maptrick changed its location to a better and faster hopefully faster server. Enjoy the site and remember the new address: <a class=\"read-more\" href=\"http://www.maptrick.org\">maptrick.org</a></p>");
	$smarty->assign('news_title01', '14.10.2008, Maptrick is back');
	
	$smarty->assign('news_text01', "<p>This day marks the first day of the return of Maptrick. The site does not offer very much yet. You can only calculate your or others coolness. But this is only the first step to a complete new Maptrick.</p><p>My name is Peter Kanzler and my Hattrick username is <a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=141589\" target=\"_blank\">DerKanzler</a>. <a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=150875\" target=\"_blank\">CHPP-ste1n</a> made me the new owner of Maptrick's CHPP license. I want to revive all other parts of the Maptrick site as well. I wanted to stay as close to the original while giving the site a new, fresh and modern look. The next feature which will be implemented will be \"The Hall of Cool\".</p><p>The server is located at my university. Let's hope it will be able to deliver good performance 24/7. Though it might not be the fastest one all around the world. The site should look good in every modern browser. This doesn't include Internet Explorer 6 obviously. If there are any problems or bugs please come to the maptrick conference and report it there.</p><p>So stay tuned, enjoy the site and remember the new address: <a class=\"read-more\" href=\"http://maptrick.ath.cx/\">maptrick.ath.cx</a></p>");
	$smarty->assign('howto_title', 'How to');
	
	$smarty->assign('howto_text', "<p><b>Link to your coolness rating:</b> http://www.maptrick.org/coolness.php?team=teamID#results. Instead of teamID use your TeamID.</p><p><b>Link to your country's Hall of Cool:</b> http://www.maptrick.org/hallofcool.php?country=countryID. Replace countryID with the ID of your country (for example 1 for Sweden).</p><p><b>Link to your league's Hall of Cool:</b> http://www.maptrick.org/hallofcool.php?league=leagueID. Replace leagueID with the ID of your league (for example 1 for Allsvenskan, the top league of Sweden).</p><p><b>Link to your federation's Hall of Cool:</b> http://www.maptrick.org/hallofcool.php?alliance=allianceID. Replace allianceID with the ID of your federation (for example 12497 for \"Flag Chasers Community\").</p><p><b>And so forth...</b><br />A direct link is presented to you in the Hall of Cool below the table. It gives you the link with which you can directly link to your options you chose. Right click on the link and choose \"Copy Link Location\" (exact wording depends on your browser and language).</p><p>Of course you can combine all of these attributes above. This is the link for all teams of the \"Flag Chasers Community\" which play in Austria, started in 2005 in terms of hosted Flags for example: http://www.maptrick.org/hallofcool.php?country=39&amp;alliance=12497&amp;year=2005&amp;term=2&amp;type=2</p>");
	$smarty->assign('credits_title', 'Credits');
	$smarty->assign('credits_text', "<p>Maptrick was originally developed by László van den Hoek (<a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=150875\" target=\"_blank\">CHPP-ste1n</a>). Many thanks to his dedication to the former Maptrick site. And my admiration to his many great ideas.</p><p><a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=404453\" target=\"_blank\">eBusiness</a> has developed the botmap feature. Great work indeed! Thank you!</p><p>Many thanks go out to <a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=176295\" target=\"_blank\">GM-teles</a> for his great PHT. It is a lifesaver indeed.</p><p>The really cool looking flags were all made by ~housewave. Check out his <a href=\"http://housewave.deviantart.com\" target=\"_blank\">DeviantArt</a> site! Thank you very much for letting me use them.</p><p>The other also superb looking icons are made by =mattahan. Take a look for more of his fine work at his <a href=\"http://mattahan.deviantart.com\" target=\"_blank\">DeviantArt</a> site. I also want to thank him for letting me use them.</p><p>I also want to thank <a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=32183\" target=\"_blank\">MD-JakobTrier</a>,  <a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=212889\" target=\"_blank\">mk0111</a> and <a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=222396\" target=\"_blank\">Vokac</a> for testing the site. A huge thanks goes out to <a href=\"http://www.hattrick.org/Common/Team/Team.aspx?teamID=1058471\" target=\"_blank\">kari1892</a> for helping me with the statistics and testing. I also do not want to forget all the people in the Maptrick conference. Your input was really worth a lot!</p>");
	$smarty->assign('about_title', 'About Maptrick');
	$smarty->assign('about_text', '<p>Maptrick is a webpage containing statistical information about countries in the on-line game, <a href="http://www.hattrick.org" target="_blank">Hattrick</a>. Maptrick was created by László van den Hoek. It is currently a work in progress.</p><p>Hattrick is a web-based football team management game, involving selling and buying players, making lineups, arranging friendly games and such.</p><p>Besides the appealing competitive aspects of the game, the community behind Hattrick is thriving. People from all over the world convene in the internal forum to discuss pretty much anything, whether related to the game or not. This community aspect drives some people to create websites with statistics about the game, and back in 2003, Maptrick was one such website. Originally envisioned as a "graphical overview of where Hattrick players live in the world", it developed into something else. Over time, it evolved the a lot of other features.</p><p>Unfortunately, Maptrick was forced to move from the server that it was hosted on, before it could be moved to the servers of fellow statistics service, <a href="http://www.alltid.org" target="_blank">Alltid</a>. The cause was simple: Maptrick was created as a hobby project, and contains lots of sub-par code and queries, and lacks good documentation, which both precipitated its ejection from the (shared) server and made it harder to port to the (already strained) Alltid servers.</p><p>The port to Alltid failed. With the planned merger between Hattrick and Alltid it was time to revive Maptrick as an independent site again.</p>');
	$smarty->assign('chpp_text', 'This application uses information from the online game service Hattrick.org. This use has been approved by Hattrick Ltd, the publishers and copyright owners of Hattrick.org. CHPP ID: 1045');
	$smarty->display('index.tpl');

	$smarty->display('footer.tpl');
}

?>